package okw.gui.wizards;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.jface.wizard.Wizard;

import okw.database.TerminalConfiguration;
import okw.database.DatabaseManager;
import okw.database.Terminal;
import okw.gui.MainWindowO;
import core.modules.configuration.ConfigurationManager;
import core.modules.configuration.Crypto;
import core.util.Converter;

public class CreateConfigurationWizardWindow extends Wizard {
	private ArrayList<Terminal> terminalList;

	protected CreateConfigurationWizardPageOne one;
	protected CreateConfigurationWizardPageTwo two;
	protected CreateConfigurationWizardPageThree three;

	public CreateConfigurationWizardWindow(ArrayList<Terminal> terminalList) {
		super();
		setWindowTitle("Generuj konfiguracje");
		setNeedsProgressMonitor(true);

		this.terminalList = terminalList;
	}

	@Override
	public void addPages() {
		one = new CreateConfigurationWizardPageOne();
		two = new CreateConfigurationWizardPageTwo();
		three = new CreateConfigurationWizardPageThree();
		
		addPage(one);
		addPage(two);
		addPage(three);
	}

	@Override
	public boolean performFinish() {
		for (ConfigurationInputGUI ciGUI : two.getSelectedTerminalsGUI()) {
			Terminal terminal = ciGUI.getTerminal();

			// Uzupelniamy dane konfiguracyjne na podstawie wprowadzonych do
			// wizarda danych
			TerminalConfiguration configuration = new TerminalConfiguration();
			configuration.setIdentificationField(terminal
					.getIdentificationField());
			configuration.setCipherKey(ciGUI.getCipherKeyText().getText());
			configuration.setIp(ciGUI.getIpText().getText());
			configuration.setPassword(ciGUI.getPasswordText().getText());

			// Konwertujemy konfiguracje do XML'a
			String ckwIP = three.getServerIPAddress(); // TODO: podmienic na
										// http://www.roseindia.net/java/java-get-example/get-own-ip.shtml
			// Klasa bazowa konfiguracji, potrzebna przy zapisie/odczycie
			// konfiguracji do/z XML'a
			core.modules.database.SuperConfiguration coreconf = new core.modules.database.SuperConfiguration(
					terminal.getIdentificationField(), ckwIP, ciGUI
							.getCipherKeyText().getText(), ciGUI
							.getPasswordText().getText());

			// Konwersja configa do XMLa
			String xmlString = Converter.objectToXML(coreconf);
			// Dodanie pierwszej linijki, potrzebnej przy enkrypcji
			xmlString = Crypto.getInstance().addVerificationLine(xmlString);
			// TODO: encryption
			xmlString = Crypto.getInstance().encrypt(xmlString,
					ciGUI.getPasswordText().getText()); // pobierane z pola,
														// zeby szyfrowac
														// haslem, a nie md5
			try {
				ConfigurationManager.getInstance().saveConfigToFile(
						coreconf.getIdentificationField(),
						coreconf.getIdentificationField(), xmlString);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			DatabaseManager.getInstance().save(configuration);
			DatabaseManager.getInstance().addTerminalConfigurationToTerminal(
					configuration, terminal);


		}
		MainWindowO.cm.aktualizacjaTerminali();
		MainWindowO.aktualizujTerminale();
		return true;
	}

	public ArrayList<Terminal> getTerminalList() {
		return terminalList;
	}
}
